<template>
  <page-header-wrapper :title="false">
    <a-card title="提交至官方">
      <template slot="extra">
        <a-button size="small" icon="redo" type="primary" @click="resetForm">刷新</a-button>
      </template>
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务名称">
                <a-input v-model="queryParam['filter[business_name]']" placeholder="请输入业务名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务编号">
                  <a-input v-model="queryParam['filter[business_no]']" placeholder="请输入业务编号"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="提交时间">
                  <a-range-picker v-model="submitTime" @change="onChangeSubmit" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="领取时间">
                  <a-range-picker v-model="receiveTime" @change="onChangeReceive" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属顾问">
                  <a-select
                    show-search
                    v-model="queryParam['filter[adviser_uid]']"
                    placeholder="输入要搜索的顾问姓名"
                    :default-active-first-option="false"
                    :show-arrow="false"
                    :filter-option="false"
                    :not-found-content="null"
                    @search="onSearch"
                  >
                    <a-select-option v-for="(item,index) in searchSource" :key="index" :value="index">
                      {{ item }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="流程状态">
                  <a-select v-model="queryParam['filter[status2]']" placeholder="请选择流程状态">
                    <a-select-option v-for="(item,index) in statusOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col v-if="group == 'small_leader'" :md="6" :sm="24">
                <a-form-model-item label="专利流程">
                  <a-select v-model="queryParam['filter[receive_uid]']" placeholder="请选择专利流程">
                    <a-select-option v-for="(item,index) in processOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="专利老师">
                  <a-select v-model="queryParam['filter[patenteer]']" placeholder="请选择专利老师">
                    <a-select-option v-for="(item,index) in patentOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="专利材料">
                  <a-select v-model="queryParam['filter[is_patenteer_file]']" placeholder="专利师是否上传材料">
                    <a-select-option :value="1">已上传</a-select-option>
                    <a-select-option :value="2">未上传</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          size="middle"
          ref="table"
          :row-key="(record) => record.cb_id"
          :columns="governmentColumns"
          :data="governmentData"
          :scroll="{ y: 500 }"
        >
          <template slot="contract_no" slot-scope="text,record">
            <a>{{text}}</a><br>
            <a>{{record.business_no}}</a>
          </template>
          <template slot="adviser" slot-scope="text,record">
            <a-tag>{{text}}</a-tag><br>
            <a-tag>{{record.group_type}}组</a-tag><br>
            <a-tag>{{record.leader}}</a-tag>
          </template>
          <template slot="receiver" slot-scope="text,record">
            <a-tag>{{text}}</a-tag><br>
            <a-tag>{{record.patenteer}}</a-tag>
          </template>
          <template slot="submit_mats_time" slot-scope="text, record">
            {{text}}<br>
            {{record.received_at}}
          </template>
          <template slot="status2" slot-scope="text,record">
            <a-button v-if="record.status2_code == 22 || record.status2_code == 20 || record.status2_code == 36" size="small" icon="edit" type="danger" @click="changeStatus(record.cb_id)">{{text}}</a-button>
            <a-button
              ghost
              v-if="record.status2_code == 40 || record.status2_code == 30"
              type="danger"
              size="small"
              @click="changeStatus(record.cb_id)"
            >
              {{text}}
            </a-button>
            <a-button
              ghost
              v-if="record.status2_code == 37"
              type="danger"
              size="small"
              @click="changeStatus(record.cb_id)"
            >
              {{text}}
            </a-button>
            <a-button
              ghost
              v-if="record.status2_code == 24 || record.status2_code == 32 || record.status2_code == 35"
              type="primary"
              size="small"
              @click="changeStatus(record.cb_id)"
            >
              {{text}}
            </a-button>
            <a-button
              ghost
              v-if="record.status2_code == 34"
              type="primary"
              size="small"
              @click="changeStatus(record.cb_id)"
            >
              {{text}}
            </a-button>
          </template>
          <template slot="patenteer_filename" slot-scope="text, record">
            <a-button v-if="text != ''" type="link" icon="download" size="small" @click="download(text, record.patenteer_file)"><ellipsis :length="25">{{text}}</ellipsis></a-button>
            <a-tag v-else color="red">暂无材料</a-tag>
          </template>
          <template slot="updated_at" slot-scope="text,record">
            <div v-if="record.status2_code == 36">
              <a-statistic-countdown :value="text * 1000 + 1000 * 60 * 60 * 24 * 1" format="D天H时m分s秒" />
            </div>
            <a-tag v-else>无需计时</a-tag>
          </template>
          <template slot="submit_mats_time" slot-scope="text,record">
            {{text}}<br>
            {{record.received_at}}
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button type="primary" size="small" @click="downloadModal(record.cb_id)">下载</a-button>
            <a-button type="default" size="small" @click="handleButtonClick(record.cb_id)">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="下载材料"
      v-model="downloadVisible"
    >
      <a-form-model
        :model="downloadForm"
      >
        <a-form-model-item label="业务名称">
          <a-input disabled v-model="downloadForm.business_name" type="text" />
        </a-form-model-item>
        <a-form-model-item label="整理后材料">
          <a-button v-if="downloadForm.mat_file" size="small" type="link" @click="download(downloadForm.mat_file)">{{downloadForm.mat_filename}}</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      centered
      v-model="submitVisible"
      title="编辑状态"
      @ok="submitChangeStatus"
      @cancel="onCancel"
    >
      <a-form-model
        ref="ruleForm"
        :model="changeStatusForm"
      >
        <a-form-model-item prop="status2">
          <a-radio-group v-model="changeStatusForm.status2" @change="onChangeStatus">
            <a-radio v-for="(item, index) in changeStatusOpts" :key="index" :value="index">
              {{item}}
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item prop="notes">
          <a-textarea :disabled="editStatus" :rows="4" v-model="changeStatusForm.notes"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import businessColumns from '@/columns/patent.process'
import {
  getGovernment,
  onCompletionAdviser,
  postOfficialEditStatus,
  getDownloadZip,
  getOfficialEditStatus
} from '@/api/patent.process'
import storage from 'store'
import { GROUP, ROLE } from '@/store/mutation-types'
import apis from '@/api'

export default {
  components: {
    STable,
    Ellipsis
  },
  name: 'Government',
  data () {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      governmentColumns: businessColumns.government,
      governmentData: parameters => {
        return getGovernment(Object.assign(parameters, this.queryParam)).then(res => {
          this.adviserOpts = res.result.adviser_opts
          this.statusOpts = res.result.status2_opts
          this.patentOpts = res.result.patenteer_opts
          this.processOpts = res.result.flow
          return res.result
        })
      },
      adviserOpts: {},
      statusOpts: {},
      patentOpts: {},
      processOpts: {},
      queryParam: {},
      advanced: true,
      downloadVisible: false,
      submitVisible: false,
      downloadForm: {},
      statusForm: {},
      remarks: '',
      searchSource: [],
      changeStatusForm: {},
      changeStatusOpts: {},
      editStatus: false,
      group: storage.get(GROUP),
      groupType: storage.get(ROLE),
      submitTime: [],
      receiveTime: []
    }
  },
  methods: {
    onChangeSubmit (date, dateString) {
      this.queryParam['filter[submit_mats_time_start]'] = dateString[0]
      this.queryParam['filter[submit_mats_time_end]'] = dateString[1]
    },
    onChangeReceive (date, dateString) {
      this.queryParam['filter[received_at_start]'] = dateString[0]
      this.queryParam['filter[received_at_end]'] = dateString[1]
    },
    downloadModal (id) {
      getDownloadZip(id).then(res => {
        this.downloadForm = res.result
        this.downloadVisible = true
      })
    },
    handleButtonClick (id) {
      if (this.groupType === 'proced_zl_engineer') {
        this.$router.push({ name: 'BusinessEdit', params: { id } })
      } else {
        this.$router.push({ name: 'AdminDetail', params: { id } })
      }
    },
    changeStatus (id) {
      const app = this
      app.changeStatusForm = {}
      app.changeStatusForm['cb_id'] = id
      getOfficialEditStatus(id).then(res => {
        if (res.status === 0) {
          app.changeStatusOpts = res.result.status2_items
          // app.changeStatusForm.status2 = res.result.contract_business.status2
          app.changeStatusForm.notes = res.result.contract_business.notes
          app.submitVisible = true
        } else {
          app.$warning({
            title: res.msg
          })
          return false
        }
      })
    },
    submitChangeStatus () {
      const app = this
      const data = {}
      data['contract_business[status2]'] = app.changeStatusForm.status2
      data['contract_business[notes]'] = app.changeStatusForm.notes
      postOfficialEditStatus(app.changeStatusForm['cb_id'], data).then(res => {
        if (res.status === 0) {
          app.onCancel()
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    // onSubmitStatus () {
    //   postGovEditStatus()
    // },
    onCancel () {
      this.changeStatusForm = {}
      this.submitVisible = false
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetForm () {
      this.queryParam = {}
      this.submitTime = []
      this.receiveTime = []
      this.$refs.table.refresh(true)
    },
    onSearch (searchText) {
      onCompletionAdviser(searchText).then(res => {
        this.searchSource = res.result.data
      })
    },
    onChangeStatus (e) {
      this.changeStatusForm.status2 = ''
      this.changeStatusForm.status2 = e.target.value
      if (e.target.value === '40') {
        this.editStatus = true
        this.changeStatusForm.notes = '专利申请文件已提交至官方，材料不可修改，请大家认真核实材料信息'
      } else {
        this.editStatus = false
        this.changeStatusForm.notes = ''
      }
    },
    download (name, url) {
      const downloadUrl = apis.Download
      const params = '?filename=' + name + '&value=' + url
      window.open(downloadUrl + params)
    }
  }
}
</script>

<style scoped>
/deep/.ant-input[disabled]{color: red;}
/deep/ .ant-statistic-content {
  font-size: 16px;
  color: #f00;
}
/deep/ .ant-table-tbody > tr.table-color-dust {
  background-color: #ffcccc;
}
</style>
